// 对user操作的model
import React from 'react';
import {Model, Effect} from 'dva-core-ts';
import {Reducer} from 'redux';
import axios from 'axios';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Toast from 'react-native-root-toast';

export interface IArticals {
  _id:string,
  artical_title:string,
  artical_detail:string,
  artical_images:{
    iid?:string,
    i_url?:string
  }[];
  artical_videos:{
    aid?:string,
    a_url?:string,
  }[],
  publish_time:string,
  type:string,
  owner_id:string
}

export interface ArticalsModelState {
  articals?:IArticals[]
}

const ARTICAL_URL = '/articals/list';
const ADDARTICAL_URL = '/addArtical';
interface ArticalModel extends Model {
  namespace: 'articals';
  state?: ArticalsModelState;
  reducers?: {
    updateState: Reducer<IArticals>;
  };
  effects?: {
    getArticalsList:Effect;
    addArtical:Effect;
  };
}
const initValue = {
  articals:[
    {
      _id:"",
      artical_title:"",
      artical_detail:"",
      artical_images:[{
        iid:"",
        i_url:""
      }],
      artical_videos:[{
        aid:"",
        a_url:"",
      }],
      publish_time:"",
      type:"",
      owner_id:""
    }
  ]
};
const articals: ArticalModel = {
  namespace: 'articals',
  state: initValue,
  reducers: {
    updateState: (state, {payload}) => {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *getArticalsList({callback}, {call, put, select}): any {
      const res = yield call(axios.get, ARTICAL_URL);
      console.log("进来了文章res",res)
      if (res.data.code === 200) {
        console.log("文章数据",res.data.data)
        yield put({
          type: 'updateState',
          payload: {
            articals: res.data.data,
          },
        });
        // const { users } = yield select(state => state);
        if (callback && typeof callback === 'function') {
          callback(res.data.data);
        }
      }
    },
    *addArtical({payload},{call,put}):any {
      console.log('发布文章的数据', payload.formData);
      // 处理formData数据
      const formData = new FormData();
      formData.append('owner_id', payload.formData.owner_id);
      formData.append('artical_title', payload.formData.artical_title);
      formData.append('artical_detail', payload.formData.artical_detail);
      formData.append('type', payload.formData.type);
      payload.formData.images && payload.formData.images.forEach((image:any) => {
        const file = {
          uri: image.uri,
          type: 'image/jpeg',
          name: image.uri.split('/').pop()
        };
        formData.append('images', file);
      });
      console.log('发布文章的formData之后的数据', formData);
      const res = yield call(axios.post, ADDARTICAL_URL, formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      });
      console.log('发布文章的接口返回的数据', res);
      if (res.data.code === 200) {
        if (payload.callback && typeof payload.callback === 'function') {
          payload.callback();
        }
      } else {
        Toast.show('发布文章失败', {
          position: Toast.positions.CENTER,
          duration: Toast.durations.LONG,
          shadow: true,
          animation: true,
        });
      }
    },
  },
};
export default articals;
